package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.game.jfcz.JfczLevelData;
import com.stark.game.jfcz.JfczLevelDataProvider;
import com.stark.game.jfcz.callback.JfczInitListener;
import com.stark.game.jfcz.callback.OnGameFinished;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityNeedleBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;

public class NeedleActivity extends BaseAc<ActivityNeedleBinding> {

    private JfczLevelDataProvider mLevelDataProvider;
    private int level;
    private boolean isPass = false;
    private Dialog myPassDialog;
    private TextView dialogTitle;
    private ImageView dialogBack, dialogImg, dialogNext;
    private Dialog myPauseDialog;
    private int max;


    @Override
    protected int onCreate() {
        return R.layout.activity_needle;
    }

    @Override
    protected void initView() {
        mDataBinding.ivNeedleBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivNeedlePause.setOnClickListener(this);

        //过关弹框
        passDialog();
        //暂停弹框
        pauseDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivNeedlePause://暂停
                myPauseDialog.show();
                mDataBinding.jfczView.pause();
                break;
            //过关弹框
            case R.id.ivDialogPassBack://返回首页
                myPassDialog.dismiss();
                finish();
                break;
            case R.id.ivDialogPassNext://重来/下一关
                myPassDialog.dismiss();
                if (isPass) {
                    level++;
                    boolean ret = updateLevelData(level);
                    if (!ret) {
                        level--;
                    }
                } else {
                    mDataBinding.jfczView.restart();
                }
                break;
            //暂停弹框
            case R.id.ivDialogPauseRestart://重新开始
                mDataBinding.jfczView.restart();
                myPauseDialog.dismiss();
                mDataBinding.jfczView.resume();
                break;
            case R.id.ivDialogPauseSelLevel://选择关卡
                SelLevelActivity.kind = 0;
                SelLevelActivity.max = max;
                Intent intent = new Intent(this, SelLevelActivity.class);
                startActivityForResult(intent, Content.SEL_LEVEL);
                myPauseDialog.dismiss();
                mDataBinding.jfczView.resume();
                break;
            case R.id.ivDialogPauseContinue://继续游戏
                myPauseDialog.dismiss();
                mDataBinding.jfczView.resume();
                break;
        }
    }

    @Override
    protected void initData() {
        initGame();
    }

    //初始化游戏
    private void initGame() {
        level = SPUtil.getInt(mContext, Content.NEEDLE_LEVEL, 1);
        mDataBinding.tvNeedleTitle.setText(getString(R.string.level_num, level));
        //初始化游戏关卡
        mDataBinding.jfczView.setInitListener(new JfczInitListener() {
            @Override
            public void onInit(int maxPointCount) {
                if (mLevelDataProvider == null) {
                    mLevelDataProvider = new JfczLevelDataProvider(maxPointCount);
                }
                mLevelDataProvider.setMaxPointCount(maxPointCount);
                max = (maxPointCount - 8) * 10;
                //更新游戏关卡数据
                updateLevelData(level);
            }
        });
        //本关游戏结束
        mDataBinding.jfczView.setOnGameFinishedListener(new OnGameFinished() {
            @Override
            public void onSuccess() {
                //过关成功
                isPass = true;
                dialogTitle.setText(getString(R.string.level_num, level));
                dialogImg.setImageResource(R.drawable.gxgg);
                dialogNext.setImageResource(R.drawable.xyy);
                myPassDialog.show();
            }

            @Override
            public void onFail() {
                //过关失败
                isPass = false;
                dialogTitle.setText(getString(R.string.level_num, level));
                dialogImg.setImageResource(R.drawable.tzsb);
                dialogNext.setImageResource(R.drawable.zsyc);
                myPassDialog.show();
            }
        });
    }

    //更新游戏关卡数据
    private boolean updateLevelData(int level) {
        JfczLevelData levelData = mLevelDataProvider.getLevelData(level);
        if (levelData == null) {
            ToastUtils.showLong(R.string.no_level_data);
            return false;
        }
        mDataBinding.jfczView.setCount(levelData.initCount, levelData.restCount);
        mDataBinding.jfczView.setRotateSpeed(levelData.rotateSpeed);
        mDataBinding.jfczView.setBiuSpeed(levelData.biuSpeed);
        //保存关卡
        SPUtil.putInt(mContext, Content.NEEDLE_LEVEL, level);
        mDataBinding.tvNeedleTitle.setText(getString(R.string.level_num, level));
        return true;
    }

    //通关弹框
    private void passDialog() {
        myPassDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_pass, null);
        myPassDialog.setContentView(inflate);
        myPassDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myPassDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialogBack = inflate.findViewById(R.id.ivDialogPassBack);
        dialogTitle = inflate.findViewById(R.id.tvDialogPassTitle);
        dialogImg = inflate.findViewById(R.id.ivDialogPassImg);
        dialogNext = inflate.findViewById(R.id.ivDialogPassNext);
        dialogBack.setOnClickListener(this);
        dialogNext.setOnClickListener(this);
    }

    //暂停弹框
    private void pauseDialog() {
        myPauseDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_pause, null);
        myPauseDialog.setContentView(inflate);
        myPauseDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myPauseDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView restart = inflate.findViewById(R.id.ivDialogPauseRestart);
        ImageView selLevel = inflate.findViewById(R.id.ivDialogPauseSelLevel);
        ImageView continueGame = inflate.findViewById(R.id.ivDialogPauseContinue);
        restart.setOnClickListener(this);
        selLevel.setOnClickListener(this);
        continueGame.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            level = data.getIntExtra(Content.level, level) + 1;
            JfczLevelData levelData = mLevelDataProvider.getLevelData(level);
            if (levelData == null) {
                ToastUtils.showLong(R.string.no_level_data);
            }
            mDataBinding.jfczView.setCount(levelData.initCount, levelData.restCount);
            mDataBinding.jfczView.setRotateSpeed(levelData.rotateSpeed);
            mDataBinding.jfczView.setBiuSpeed(levelData.biuSpeed);
            mDataBinding.tvNeedleTitle.setText(getString(R.string.level_num, level));
        }
    }

}